package com.mega.fe.handler;

import com.mega.fe.Unravel;
import com.mega.fe.render.RendererUtils;
import com.mega.fe.util.BaseInit;
import com.mega.fe.util.client.MegaFont;
import com.mojang.blaze3d.vertex.BufferUploader;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

import javax.annotation.Nullable;
import java.util.Objects;

@Mod.EventBusSubscriber
public class EventHandler {
    @Mod.EventBusSubscriber
    public static class Client {
        public static int i = 1;
        public static int lastI = 0;
        public static boolean isFETooltipRendering = false;
        static Minecraft mc = Minecraft.getInstance();

        @OnlyIn(Dist.CLIENT)
        public static void update100() {
        }

        @SubscribeEvent
        public static void onGuiOpen(ScreenEvent.Opening event) {
            if (BaseInit.clientGod() && BaseInit.mayDeathScreen(event.getCurrentScreen()))
                event.setCanceled(true);
        }

        @SuppressWarnings("DataFlowIssue")
        @SubscribeEvent
        public static void onGuiOpen(ScreenEvent.Render.Pre event) {
            //System.out.println(isFETooltipRendering);
            //if (BaseInit.isGod(mc.player))
            //    RendererUtils.renderBackground();
            if (BaseInit.clientGod() && BaseInit.mayDeathScreen(event.getScreen())) {
                mc.mouseHandler.grabMouse();
                LocalPlayer player = mc.player;
                player.deathTime = 0;
                player.hurtTime = 0;
                player.respawn();
                event.setCanceled(true);
            }
        }

        @SubscribeEvent(priority = EventPriority.HIGHEST)
        public static void onGameRendering(TickEvent.RenderTickEvent event) {

            //System.out.println(EventHandler.Client.isFETooltipRendering);

            //if (BaseInit.isGod(mc.player)) {
            //    RendererUtils.renderBackground();
            //}
            if (BaseInit.isDeath(mc.player) && event.phase == TickEvent.Phase.END) {

                DeathScreen screen = new DeathScreen(Component.literal("「梦幻 • 终点」 "), false) {
                    @Override
                    public void render(GuiGraphics p_283488_, int p_283551_, int p_283002_, float p_281981_) {
                        font = MegaFont.FantasyFont;
                        p_283488_.fillGradient(0, 0, this.width, this.height, 1615855616, -1602211792);
                        p_283488_.pose().pushPose();
                        p_283488_.pose().scale(2.0F, 2.0F, 2.0F);
                        p_283488_.drawCenteredString(this.font, this.title, this.width / 2 / 2, 30, 16777215);
                        p_283488_.pose().popPose();
                        p_283488_.drawCenteredString(this.font, Component.literal("「梦幻 • 终点」 "), this.width / 2, 85, 16777215);
                        p_283488_.drawCenteredString(this.font, "", this.width / 2, 100, 16777215);
                        if (p_283002_ > 85) {
                            Objects.requireNonNull(this.font);
                            if (p_283002_ < 85 + 9) {
                                Style $$4 = this.getClickedComponentStyleAt(p_283551_);
                                p_283488_.renderComponentHoverEffect(this.font, $$4, p_283551_, p_283002_);
                            }
                        }

                        for (Renderable renderable : this.renderables) {
                            renderable.render(p_283488_, p_283551_, p_283002_, p_281981_);
                        }

                    }

                    @Nullable
                    private Style getClickedComponentStyleAt(int p_95918_) {
                        int $$1 = mc.font.width(Component.literal("「梦幻 • 终点」 "));
                        int $$2 = this.width / 2 - $$1 / 2;
                        int $$3 = this.width / 2 + $$1 / 2;
                        return p_95918_ >= $$2 && p_95918_ <= $$3 ? mc.font.getSplitter().componentStyleAtWidth(Component.literal("「梦幻 • 终点」 "), p_95918_ - $$2) : null;
                    }
                };
                ForgeHooksClient.clearGuiLayers(mc);
                screen.added();
                BufferUploader.reset();
                mc.mouseHandler.releaseMouse();
                KeyMapping.releaseAll();
                screen.init(mc, mc.window.getGuiScaledWidth(), mc.window.getGuiScaledHeight());
                screen.render(new GuiGraphics(Minecraft.getInstance(), Minecraft.getInstance().renderBuffers().bufferSource()), (int) mc.mouseHandler.xpos(), (int) mc.mouseHandler.ypos(), Minecraft.getInstance().getPartialTick());
                mc.noRender = false;

                mc.updateTitle();
            }
        }


        @SubscribeEvent
        @OnlyIn(Dist.CLIENT)
        public static void onClientSetup(FMLClientSetupEvent event) {
            Unravel.out("client is starting:" + event.description());
            mc.getWindow().setTitle("Minecraft 1.20.1-FantasyEnding");
        }
    }

    @Mod.EventBusSubscriber
    public static class Server {
        @SubscribeEvent
        public static void onPlayerDied(LivingDeathEvent event) {
        }
    }
}
